<template>
  <div>
    <el-divider content-position="center">主题颜色</el-divider>
    <div class="themebox">
      <div
        class="themeItem"
        v-for="(item, index) in themeItem"
        :key="index"
        :style="{ backgroundColor: item.hex }"
        @click="changeTheme(item)"
      ></div>
    </div>
  </div>
</template>

<script>
import { updateTheme } from '@/api/sys/user'
// import { mapState } from 'vuex'
export default {
  name: 'ThemeContent',

  data() {
    return {
      themeItem: [
        { hex: '#409eff', className: 'theme-blue' },
        { hex: '#3f6f8b', className: 'theme-green' },
        { hex: '#7a2650', className: 'theme-red' },
        { hex: '#722ed1', className: 'theme-purple' },
      ],
      accountId: '',
      // themeItem: ['#409eff', '#3f6f8b', '#7a2650', '#722ed1']
    }
  },

  mounted() {
    this.accountId = localStorage.getItem('accountId')
  },
  computed: {
    // ...mapState(['userId']),
  },

  methods: {
    changeTheme(item) {
      updateTheme({ theme: item.hex, accountId: this.accountId }).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: '主题修改成功',
            type: 'success',
          })
          document.documentElement.style.setProperty('--menuItemHoverBackgroundColor', item.hex)
          localStorage.setItem('theme', item.hex)
        } else {
          this.$message({
            message: res.data.message,
            type: 'warning',
          })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.themebox {
  display: flex;
  justify-content: space-around;
  .themeItem {
    cursor: pointer;
    border-radius: 2px;
    margin: 0 15px;
    width: 20px;
    height: 20px;

    // &:nth-child(1) {
    //   background: #409eff;
    // }
    // &:nth-child(2) {
    //   background: #13c2c2;
    // }
    // &:nth-child(3) {
    //   background: #f5222d;
    // }
    // &:nth-child(4) {
    //   background: #722ed1;
    // }
  }
}
</style>
